﻿Imports System.Globalization
Imports ZLDNN.DNN.Modules.MultiMediaGalleryPro.Business
Imports Aricie.DNN.Services.Filtering

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Providers


    Public Class MultiMediaGalleryUrlRewriter
        Implements IUrlRewriterProvider
        Implements IIsActive


        Public Function GetModuleRewrites() As List(Of GroupParamsRewrite) Implements IUrlRewriterProvider.GetRewrites
            Dim toReturn As New List(Of GroupParamsRewrite)

            Dim groupMediaTitle As New GroupParamsRewrite("MediaTitle", "Title of the current media", RewriteType.SubPath)
            groupMediaTitle.UrlParams.Add(UrlParam.DNNParams, ParamNature.Mandatory Or ParamNature.Exclusive)
            groupMediaTitle.UrlParams.Add(UrlParam.FromSyntax("itemid"), ParamNature.Mandatory Or ParamNature.Exclusive)
            groupMediaTitle.UrlParams.Add(UrlParam.FromSyntax("albumid"), ParamNature.NonExclusive)
            groupMediaTitle.UrlParams.Add(UrlParam.FromSyntax("selectedmoduleid"), ParamNature.NonExclusive)
            toReturn.Add(groupMediaTitle)

            Dim groupAlbumName As New GroupParamsRewrite("AlbumName", "Name of the current album", RewriteType.SubPath)
            groupAlbumName.UrlParams.Add(UrlParam.DNNParams, ParamNature.Mandatory Or ParamNature.Exclusive)
            groupAlbumName.UrlParams.Add(UrlParam.FromSyntax("albumid"), ParamNature.NonExclusive)
            groupAlbumName.UrlParams.Add(UrlParam.FromSyntax("selectedmoduleid"), ParamNature.NonExclusive)
            toReturn.Add(groupAlbumName)

            Dim groupParentAlbumName As New GroupParamsRewrite("ParentAlbumName", "Name of the current album", RewriteType.SubPath)
            groupParentAlbumName.UrlParams.Add(UrlParam.DNNParams, ParamNature.Mandatory Or ParamNature.Exclusive)
            groupParentAlbumName.UrlParams.Add(UrlParam.FromSyntax("albumid"), ParamNature.NonExclusive)
            groupParentAlbumName.UrlParams.Add(UrlParam.FromSyntax("selectedmoduleid"), ParamNature.NonExclusive)
            toReturn.Add(groupParentAlbumName)
            Return toReturn
        End Function

        Public Function RewriteParams(ByVal groupName As String, ByVal objRewriteType As RewriteType, _
                                      ByVal params As Dictionary(Of UrlParam, String), ByVal behaviour As FriendlierUrlStrategy) As GroupRewriteResult _
                                      Implements IUrlRewriterProvider.RewriteParams
            Dim toReturn As New GroupRewriteResult()
            If params.ContainsKey(UrlParam.FromSyntax("albumid")) Then
                Dim albumId As Integer = Integer.Parse(params(UrlParam.FromSyntax("albumid")), CultureInfo.InvariantCulture)

                If params.ContainsKey(UrlParam.FromSyntax("selectedmoduleid")) Then
                    Dim itemMid As Integer = Integer.Parse(params(UrlParam.FromSyntax("selectedmoduleid")), CultureInfo.InvariantCulture)

                    Select Case groupName.ToLowerInvariant
                        Case "mediatitle"
                            If params.ContainsKey(UrlParam.FromSyntax("itemid")) Then
                                Dim itemId As Integer = Integer.Parse(params(UrlParam.FromSyntax("itemid")), CultureInfo.InvariantCulture)
                                toReturn.RewriteValue = GetMediaTitle(itemId, itemMid, behaviour)
                                toReturn.ConsumedParameters(UrlParam.FromSyntax("itemid")) = True
                            End If
                        Case "albumname"
                            toReturn.RewriteValue = GetAlbumName(albumId, itemMid, behaviour)
                            toReturn.ConsumedParameters(UrlParam.FromSyntax("albumid")) = True
                        Case "parentablumname"
                            toReturn.RewriteValue = GetParentAlbumName(albumId, itemMid, behaviour)
                            toReturn.ConsumedParameters(UrlParam.FromSyntax("albumid")) = True
                    End Select

                End If
            End If

            Return toReturn
        End Function

        Private Function GetMediaTitle(ByVal itemId As Integer, ByVal selectedMid As Integer, ByVal behaviour As FriendlierUrlStrategy) As String
            Dim myCtlr As New MultiMediaGalleryProController
            Dim myObj As MultiMediaGalleryProInfo = myCtlr.Get(itemId, selectedMid)
            'Return behaviour.StringEscaper.EscapeString(myObj.Title)
            Return myObj.Title
        End Function

        Private Function GetAlbumName(ByVal albumId As Integer, ByVal selectedMid As Integer, ByVal behaviour As FriendlierUrlStrategy) As String
            Dim myCtlr As New MediaCatalogController
            Dim myObj As MediaCatalogInfo = myCtlr.Get(albumId, selectedMid)
            'Return behaviour.StringEscaper.EscapeString(myObj.Title)
            Return myObj.Title
        End Function

        Private Function GetParentAlbumName(ByVal albumId As Integer, ByVal selectedMid As Integer, ByVal behaviour As FriendlierUrlStrategy) As String
            Dim myCtlr As New MediaCatalogController
            Dim myObj As MediaCatalogInfo = myCtlr.Get(albumId, selectedMid)
            Dim myParentObj As MediaCatalogInfo = myCtlr.Get(myObj.ParentID, selectedMid)
            'Return behaviour.StringEscaper.EscapeString(myParentObj.Title)
            Return myParentObj.Title
        End Function

        Public ReadOnly Property IsActive As Boolean Implements IIsActive.IsActive
            Get
                'ZLDNN.DNN.Modules.MultiMediaGalleryPro.Business.MediaCatalogController
                Return DotNetNuke.Framework.Reflection.CreateType("ZLDNN.DNN.Modules.MultiMediaGalleryPro.Business.MediaCatalogController", True) IsNot Nothing
            End Get
        End Property
    End Class

End Namespace